﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp;
using Abp.Auditing;
using DHJ.Cost.Tenants.Dashboard.Dto;
using DHJ.Cost.Workshops;

namespace DHJ.Cost.Tenants.Dashboard
{
    [DisableAuditing]
    public class TenantDashboardAppService : CostAppServiceBase, ITenantDashboardAppService
    {
        private const bool UsingFakeData = true;

        public async Task<DashCostOutput> GetDashCost()
        {

            if (UsingFakeData)
            {
                return await GetFakeDashCost();
            }

            return new DashCostOutput();
        }

        private async Task<DashCostOutput> GetFakeDashCost()
        {
            var output = new DashCostOutput
            {
                Costs =
                    new List<DashCost>()
                    {
                        new DashCost()
                        {
                            CostType = "原材料成本",
                            Value = 45644.15m,
                            Data = DashboardRandomDataGenerator.GetDecimalRandomArray(12, 1000, 2000).ToList()
                        },
                        new DashCost()
                        {
                            CostType = "工时成本",
                            Value = 25456.78m,
                            Data = DashboardRandomDataGenerator.GetDecimalRandomArray(12, 500, 1000).ToList()
                        },
                        new DashCost()
                        {
                            CostType = "工装成本",
                            Value = 78456.78m,
                            Data = DashboardRandomDataGenerator.GetDecimalRandomArray(12, 1500, 3000).ToList()
                        },
                        new DashCost()
                        {
                            CostType = "外协产品成本",
                            Value = 14456.78m,
                            Data = DashboardRandomDataGenerator.GetDecimalRandomArray(12, 500, 1500).ToList()
                        }
                    },
                Income = new DashIncome()
                {
                    TemporaryPrice = 46545.145m,
                    ContractPrice = 78894.45m,
                    TotalReceipts = 82863.69m,
                    TotalSettlement = 92512.49m,
                    ChartData = new List<DashChartData>()
                    {
                        new DashChartData()
                        {
                            Name = "去年",
                            Series = new List<NameValue<decimal>>()
                            {
                                new NameValue<decimal>("一月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("二月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("三月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("四月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("五月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("六月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("七月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("八月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("九月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("十月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("十一月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("十二月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            }
                        },
                        new DashChartData()
                        {
                            Name = "今年",
                            Series = new List<NameValue<decimal>>()
                            {
                                new NameValue<decimal>("一月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("二月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("三月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("四月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("五月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("六月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("七月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("八月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("九月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("十月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("十一月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                new NameValue<decimal>("十二月", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            }
                        }
                    }
                }
            };


            return output;
        }


        public async Task<List<DashModelOutput>> GetDashModel()
        {
            if (UsingFakeData)
            {
                return await GetFakeDashModel();
            }

            return new List<DashModelOutput>();
        }

        private async Task<List<DashModelOutput>> GetFakeDashModel()
        {
            var models = new List<string>()
            {
               "CZ-3A",
               "CZ-3B",
               "CZ-5",
               "CZ-5B",
               "CZ-7",
               "CZ-7A",
               "CZ-8",
            };

            var outputs = new List<DashModelOutput>();

            foreach (var model in models)
            {
                outputs.Add(new DashModelOutput()
                {
                    Result1 = new DashModelResult1()
                    {
                        Title = model,
                        Desc = "收入支出对比",
                        Data = new List<DashChartData>()
                        {
                            new DashChartData("一月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("二月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("三月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("四月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("五月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("六月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("七月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("八月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("九月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("十月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("十月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("十月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("十一月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                            new DashChartData("十二月",
                                new List<NameValue<decimal>>()
                                {
                                    new NameValue<decimal>("收入",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                                    new NameValue<decimal>("支出",
                                        DashboardRandomDataGenerator.GetRandomInt(10000, 50000))
                                }),
                        }
                    },
                    Result2 = new DashModelResult2()
                    {
                        Title = model,
                        Desc = "成本项分布",
                        Data = new List<NameValue<decimal>>()
                        {
                            new NameValue<decimal>("原材料成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("辅助材料成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("标准件成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("外协产品成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("工装成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("燃动成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("人工成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("制造成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("其他直接成本", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                        }
                    }
                });
            }


            return outputs;
        }

        public async Task<List<DashWorkshopOutput>> GetDashWorkshop()
        {
            if (UsingFakeData)
            {
                return await GetFakeDashWorkshop();
            }

            return new List<DashWorkshopOutput>();
        }

        private async Task<List<DashWorkshopOutput>> GetFakeDashWorkshop()
        {
            var workshops = new List<string>()
            {
                "51车间", "53车间", "55车间", "56车间", "57车间", "58车间"
            };

            var outputs = new List<DashWorkshopOutput>();

            foreach (var workshop in workshops)
            {
                outputs.Add(new DashWorkshopOutput()
                {
                    Result1 = new DashWorkshopResult1()
                    {
                        Title = workshop,
                        Desc = "型号成本",
                        Data = new List<NameValue<decimal>>()
                        {
                            new NameValue<decimal>("CZ-3A", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("CZ-3B", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("CZ-5", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("CZ-5B", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("CZ-7", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("CZ-7A", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),
                            new NameValue<decimal>("CZ-8", DashboardRandomDataGenerator.GetRandomInt(10000, 50000)),

                        }
                    },
                    Result2 = new DashWorkshopResult2()
                    {
                        Title = workshop,
                        Desc = "成本趋势",
                        Data = new List<NameValue<List<decimal>>>()
                        {
                            new NameValue<List<decimal>>("原材料成本",
                                DashboardRandomDataGenerator.GetDecimalRandomArray(12, 5000, 10000).ToList()),
                            new NameValue<List<decimal>>("工时成本",
                                DashboardRandomDataGenerator.GetDecimalRandomArray(12, 5000, 10000).ToList()),
                            new NameValue<List<decimal>>("工装成本",
                                DashboardRandomDataGenerator.GetDecimalRandomArray(12, 5000, 10000).ToList()),
                            new NameValue<List<decimal>>("外协产品成本",
                                DashboardRandomDataGenerator.GetDecimalRandomArray(12, 5000, 10000).ToList()),
                        }
                    }
                });
            }

            return outputs;
        }

        public async Task<DashAnnualOutput> GetDashAnnual()
        {
            if (UsingFakeData)
            {
                return await GetFakeDashAnnual();
            }

            return new DashAnnualOutput();
        }

        private async Task<DashAnnualOutput> GetFakeDashAnnual()
        {
            var output = new DashAnnualOutput
            {
                Data = new List<DashAnnual>()
                {
                    new DashAnnual()
                    {
                        Name = "一月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "二月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "三月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "四月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "五月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "六月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "七月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "八月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "九月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "十月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "十一月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                    new DashAnnual()
                    {
                        Name = "十二月",
                        Values = new List<decimal>()
                        {
                            DashboardRandomDataGenerator.GetRandomInt(10000, 50000),
                            DashboardRandomDataGenerator.GetRandomInt(-100, 100)
                        }
                    },
                }
            };


            return output;
        }

        public async Task<DashChangeOutput> GetDashChange()
        {
            if (UsingFakeData)
            {
                return await GetFakeDashChange();
            }

            return new DashChangeOutput();
        }

        private async Task<DashChangeOutput> GetFakeDashChange()
        {
            try
            {
                var output = new DashChangeOutput
                {
                    ProductChanges = new List<DashProductChange>(), EventChanges = new List<DashEventChange>()
                };


                for (int i = 1; i <= 5; i++)
                {
                    output.ProductChanges.Add(new DashProductChange()
                    {
                        TaskNum = "XXX-XXX-00" + i,
                        DrawingNum = "XXX-XXX-00" + i,
                        ProductName = "XXX-XXX-00" + i,
                        Batch = "XXX-XXX-00" + i,
                        Change = DashboardRandomDataGenerator.GetRandomInt(-1000, 1000)
                    });

                    output.EventChanges.Add(new DashEventChange()
                    {
                        Name = "导入成本",
                        User = "Admin",
                        Desc = "原材料成本",
                        Time = DateTime.Now.AddHours(0 - i)
                    });
                }

                return output;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

        }

        public async Task<DashWorkshopChangeOutput> GetWorkshopDashChange(Workshop workshop)
        {
            if (UsingFakeData)
            {
                return await GetFakeWorkshopDashChange();
            }

            return new DashWorkshopChangeOutput();
        }

        private async Task<DashWorkshopChangeOutput> GetFakeWorkshopDashChange()
        {
            try
            {
                var output = new DashWorkshopChangeOutput
                {
                    ProductChanges = new List<DashProductChange>(), ProductValues = new List<DashProductChange>()
                };


                for (int i = 1; i <= 5; i++)
                {
                    output.ProductChanges.Add(new DashProductChange()
                    {
                        TaskNum = "XXX-XXX-00" + i,
                        DrawingNum = "XXX-XXX-00" + i,
                        ProductName = "XXX-XXX-00" + i,
                        Batch = "XXX-XXX-00" + i,
                        Change = DashboardRandomDataGenerator.GetRandomInt(-1000, 1000)
                    });

                    output.ProductValues.Add(new DashProductChange()
                    {
                        TaskNum = "XXX-XXX-00" + i,
                        DrawingNum = "XXX-XXX-00" + i,
                        ProductName = "XXX-XXX-00" + i,
                        Batch = "XXX-XXX-00" + i,
                        Change = DashboardRandomDataGenerator.GetRandomInt(10000, 50000)
                    });
                }

                return output;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

        }
    }
}